package com.kgc.controller;

import com.kgc.common.Result;
import com.kgc.entity.Message;
import com.kgc.service.interf.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/apis/user/message")
public class MessageController {
    @Autowired
    MessageService messageService;

    /**
     * 发送留言
     * @param message
     * @return
     */
    @RequestMapping("/upload")
    public String addMessage(Message message){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
        message.setTime(simpleDateFormat.format(new Date()));
        int i = messageService.addMessage(message);
        if (i>0){
            return Result.success("200","留言成功");
        }else {
            return Result.success("400","留言失败");
        }
    }

    /**
     * 查询留言
     * @param
     * @return
     */
    @RequestMapping("/query")
    public String queryMessage(){
        return Result.success("200","查询成功",messageService.queryMessage());
    }

    /**
     * 删除留言
     * @param messageId
     * @return
     */
    @RequestMapping("/delById")
    public String deleteMessage(Integer messageId){
        int i = messageService.deleteMessage(messageId);
        if (i>0){
            return Result.success("200","删除成功");
        }else {
            return Result.success("400","删除失败");
        }
    }
}
